package cc.rengu.igas.share.core.service.asyncall;

import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * TODO d0异步记账异步处理
 */
public class AsynMchntD0AccoutingService extends DubboAsynBaseService {

    @BIZ_PROCESS
    public void mchntD0Accouting() throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysNo;
        String instId;
        /*判断记账结果，更新支付订单状态*/
        if (!xmlTreeUtil.isExist(TreeNodeConstant.DUBBO_RESPONSE)) {
            rglog.error("dubbo调用d0记账失败");
            return;
        }
        rglog.debug("dubbo调用正常返回");
        //TODO 调用成功后进行记账处理，由项目组实现该交易
//        Result<JSONObject> responseResult = (Result<JSONObject>) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, Result.class);
//        D0RealTimeEntryToBankAcctResponse response = JSON.toJavaObject(responseResult.getResult(), D0RealTimeEntryToBankAcctResponse.class);
//        rglog.info("d0入账结果<{}>", JSON.toJSONString(responseResult));
//        if (response.getChnlMchntInfo() == null || response.getChnlMchntInfo().getMchntNo() == null) {
//            rglog.error("未知商户的d0入账");
//            return;
//        }

        /*获取支付订单*/
//        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
//        PayOrderInfo payOrderInfo = payOrderInfoMapper.selectPayOrderInfoByChannelReconKey(response.getHeader().getInstId(), ChannelEnum.ESB.getChannelType(), response.getHeader().getTraceNo());
//        if (payOrderInfo == null) {
//            rglog.error("根据机构号<{}>、通道id<{}>、对账流水号<{}>获取支付订单列表失败", response.getHeader().getInstId(), ChannelEnum.ESB.getChannelType(), response.getHeader().getTraceNo());
//            return;
//        }
//
//        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
//        List<MchntOrderInfo> mchntOrderInfoList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(payOrderInfo.getInstId(), payOrderInfo.getSysSeqNum());
//        if (mchntOrderInfoList == null || mchntOrderInfoList.size() == 0) {
//            rglog.info("商户订单信息表中无该笔订单,instId:<{}>,sysSeqNum:<{}>", payOrderInfo.getInstId(), payOrderInfo.getSysSeqNum());
//            return;
//        }
//        mchntOrderInfoList = mchntOrderInfoList.stream().filter(item -> item.getMchntNo().equals(response.getChnlMchntInfo().getMchntNo())).collect(Collectors.toList());
//        if (mchntOrderInfoList.size() != 1) {
//            rglog.error("查询商户<{}>的订单异常", response.getChnlMchntInfo().getMchntNo());
//            return;
//        }

//        MchntOrderInfo mchntOrderInfo = mchntOrderInfoList.get(0);
//        if (responseResult.isSuccess() && OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(response.getRespCode())) {

//            /*入账成功，更新交易状态*/
//            mchntOrderInfo.setRealSettleStatus("00");
//            mchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
//            mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//            payOrderInfo.setSettleDate(response.getChnlPayInfo().getSettleDate());
//            payOrderInfo.setChannelSeqNum(response.getChnlPayInfo().getChannelSeqNum());
//            payOrderInfo.setChannelOrderId(response.getChnlPayInfo().getChannelOrderId());
//            payOrderInfo.setRespCode(response.getRespCode());
//            payOrderInfo.setRespDesc(response.getRespDesc());
//            payOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
//            payOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
//            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//        } else if (OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode().equals(response.getRespCode())) {
//
//            /*入账未知，更新交易状态*/
//            mchntOrderInfo.setRealSettleStatus("02");
//            mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//            payOrderInfo.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
//            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//        } else {
//            /*入账失败，更新交易状态*/
//            mchntOrderInfo.setRealSettleStatus("01");
//            mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//            payOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
//        }
//
//        OrderService orderService = new OrderServiceImpl();
//        if (!orderService.updateOrderInfo(null, java.util.Arrays.asList(mchntOrderInfo), payOrderInfo)) {
//            rglog.error("更新订单表状态失败");
//            return;
//        }

    }

}
